﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using RayTracer.Structures;
using System.Xml;

namespace RayTracer.Environment.Lights
{
    /// <summary>
    /// Modelliert ein Licht.
    /// </summary>
    [Serializable]
    public abstract class ALight 
    {
        #region Attribute
        private Structures.Point position;
        private Color color;
        #endregion Attribute

        #region Eigenschaften
        /// <summary>
        /// Gibt und setzt die Position.
        /// </summary>
        public Structures.Point Position { get { return position; } set { position = value; } }

        /// <summary>
        /// Gibt und setzt die Farbe.
        /// </summary>
        public Color Color { get { return color; } set { color = value; } }
        #endregion Eigenschaften

        #region Methoden
        #region Typenumwandlung
        /// <summary>
        /// Gibt eine Zeichenkettenrepräsentation der Klasse zurück.
        /// </summary>
        /// <returns>Die Zeichenkette.</returns>
        public override string ToString()
        {
            return "Licht @ " + Position.ToString() + " Color: " + color.ToString();
        }

        /// <summary>
        /// Erstellt ein Licht aus einem XML-Knoten.
        /// </summary>
        /// <param name="xmlNode">Das XML-Knoten.</param>
        /// <param name="obj">Das spezifische Licht.</param>
        /// <returns>Das Licht.</returns>
        internal static T FromXml<T>(XmlNode xmlNode, T obj) where T : ALight
        {
            XmlNode nodePosition = xmlNode.SelectSingleNode("Position");
            XmlNode nodeColor = xmlNode.SelectSingleNode("Color");
            if (nodePosition != null) obj.position = global::RayTracer.Structures.Point.FromXml(nodePosition);
            if (nodeColor != null) obj.color = NetXmlExtensions.ColorFromXml(nodeColor);
            return obj;
        }

        /// <summary>
        /// Wandelt das Licht in einen XML-Knoten um.
        /// </summary>
        /// <param name="xmlDocument">Das zugrunde liegende XML-Dokument.</param>
        /// <returns>Der XML-Knoten.</returns>
        internal virtual XmlNode ToXml(XmlDocument xmlDocument)
        {
            XmlNode node = xmlDocument.CreateNode(XmlNodeType.Element, this.GetType().Name, null);
            node.AppendChild(position.ToXml(xmlDocument, "Position"));
            node.AppendChild(color.ToXml(xmlDocument, "Color"));
            return node;
        }
        #endregion Typenumwandlung
        #endregion Methoden
    }
}
